<?php

require_once '../Config.php';
require_once '../alipay-sdk/aop/AopClient.php';
require_once '../alipay-sdk/aop/request/AlipayOpenAuthTokenAppRequest.php';

class AliIsv extends Config{
    public function request(){
        if(isset($_GET['app_auth_code'])){
            $code=$_GET['app_auth_code'];
            $aop = new AopClient ();
            $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
            $aop->appId = $this->app_id;
            $aop->rsaPrivateKey = $this->privateKey;
            $aop->alipayrsaPublicKey=$this->publicKey;
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset='UTF-8';
            $aop->format='json';
            $request = new AlipayOpenAuthTokenAppRequest ();
            $request->setBizContent(json_encode([
                'grant_type'=>'authorization_code',
                'code'=>$code
            ]));
            $result = $aop->execute ($request);

            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            if(!empty($resultCode)&&$resultCode == 10000){
                file_put_contents($this->authDataPath,json_encode($result));
                echo "<pre>";print_r($result);echo "<pre>";
            } else {
                echo "失败";
            }
        }
    }
}

(new AliIsv())->request();